package com.ivo.quartz;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.ivo.biz.MajorAddition;
import com.ivo.biz.MajorAdditionManager;
import com.ivo.biz.MajorRule;
import com.ivo.biz.MajorRuleService;
import com.ivo.biz.PerformanceService;
import com.ivo.sys.Code;
import com.ivo.sys.CodeService;

/**
 * 
 *
 * @author Minkey Wang
 * @version 2018年1月10日
 */
@Repository
public class MajorAdditionQuartz {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
    @Autowired
    private PerformanceService performanceService;
	
	@Autowired
	private CodeService codeService;
	
    @Autowired
    private MajorAdditionManager majorAdditionManager;
    
    
    @Autowired
    private MajorRuleService majorRuleService;
    
    @Transactional
    @Scheduled(cron = "0 20 7 16 * ? ")//每月16日上午07:20触发
	public void execute() {
		
		Calendar calendar = Calendar.getInstance(); 
		SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
		String str = sdf.format(calendar.getTime());
		String month = str.substring(0, 7);
		
	    RestTemplate restTemplate = new RestTemplate();
	    Gson googleJson = new Gson();
	    JsonParser parser = new JsonParser();
		
		//同步专业加给
	    logger.info("同步专业加给start");
		String majorurl = "http://10.20.2.188:5680/NewETS/jcom/getPosToplvl";
		String majorjson = restTemplate.getForObject(majorurl, String.class);
		JsonArray majorJarray   = parser.parse(majorjson).getAsJsonArray();
		List<Operation> jsonMajorList =  googleJson.fromJson(majorJarray, new TypeToken<List<Operation>>() { }.getType());
		for(Operation majorAddition:jsonMajorList){
			MajorAddition maorAdditionTemp = performanceService.getMajorAddition(month,majorAddition.getUser_id());
			if(maorAdditionTemp==null){
				MajorAddition major = new MajorAddition();
				Code code= codeService.getCode(majorAddition.getOperation_id());
				if(code!=null){
					Long id =code.getId(); 
					major.setOpe_fk(id);
				}
				major.setFactory(majorAddition.getFab());
				major.setMonth(month);
				major.setEmp_id(majorAddition.getUser_id());
				major.setEmp_name(majorAddition.getUserName());
				major.setExam_name(majorAddition.getExam_sub());
				major.setLevel(majorAddition.getExam_level());
				major.setSeniority(majorAddition.getWorkYear());
				Date date;
				try {
					if(!majorAddition.getDateOfComp().equals("")){
						date = sdf.parse(majorAddition.getDateOfComp());
						if(date!=null)
							major.setEffevtive_date(date);
					}
				} catch (ParseException e) {
					e.printStackTrace();
				}
				MajorRule mr = majorRuleService.getMajorRule(majorAddition.getFab(), majorAddition.getExam_level());
				if(mr!=null)
					major.setAmount(mr.getAmount());
				majorAdditionManager.save(major);
			}else {
				maorAdditionTemp.setFactory(majorAddition.getFab());
				maorAdditionTemp.setExam_name(majorAddition.getExam_sub());
				maorAdditionTemp.setLevel(majorAddition.getExam_level());
				maorAdditionTemp.setSeniority(majorAddition.getWorkYear());
				MajorRule mr = majorRuleService.getMajorRule(majorAddition.getFab(), majorAddition.getExam_level());
				if(mr!=null)
					maorAdditionTemp.setAmount(mr.getAmount());
				Date date;
				try {
					if(!majorAddition.getDateOfComp().equals("")){
						date = sdf.parse(majorAddition.getDateOfComp());
						if(date!=null)
							maorAdditionTemp.setEffevtive_date(date);
					}
				} catch (ParseException e) {
					e.printStackTrace();
				}
				majorAdditionManager.save(maorAdditionTemp);
			}
		}
		logger.info("同步专业加给end");
	}

}
